package com.etime.shyauth.controller;

import com.etime.shyauth.config.BaseMessage;
import com.etime.shyauth.config.ListMessage;
import com.etime.shyauth.dto.SysRoleRightDto;
import com.etime.shyauth.services.interfaces.SysRoleRightService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色权限分配controller层
 */
@Controller
@Api(tags = "角色权限分配controller层")
@RequestMapping("/sys/right")
@ResponseBody
public class SysRoleRightController {

    @Autowired
    private SysRoleRightService service;

    @ApiOperation("初始化角色权限分配的数据接口")
    @ApiImplicitParam("角色id")
    @GetMapping("/get/{roleid}")
    @ApiResponse(code = 200, message = "rrid=null，表示该角色没有此功能的权限")
    public ListMessage<SysRoleRightDto> selectRoleRightList(@PathVariable("roleid") Integer roleid){
        List<SysRoleRightDto> list = service.selectSysRoleRgihtDtoList(roleid);
        ListMessage<SysRoleRightDto> msg = new ListMessage<>();
        if(list!=null){
            msg.setCode(1);
            msg.setDatalist(list);
        }else{
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("保存角色权限数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleid",value = "角色id"),
            @ApiImplicitParam(name="funids",value = "角色所能使用的功能id数组")
    })
    @PostMapping("/save/{roleid}")
    public BaseMessage saveRoleRight(@PathVariable("roleid") Integer roleid,@RequestParam List<Integer> funids){
        BaseMessage msg = new BaseMessage();
        try {
            int rtn = service.saveRoleRight(roleid,funids);
            msg.setCode(rtn);
        } catch (Exception e) {
            msg.setCode(0);
            msg.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return msg;
    }

}

